package cn.com.b2office.struts2.action;

import java.net.URLDecoder;
import java.util.Map;
import java.util.regex.Pattern;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;

import cn.com.b2office.domain.Cart;
import cn.com.b2office.domain.User;
import cn.com.b2office.service.CartService;
import cn.com.b2office.service.UserService;

import com.opensymphony.xwork2.ActionSupport;

public class ValidateLoginAction extends ActionSupport implements SessionAware,
		ServletRequestAware {
	private static final long serialVersionUID = 3980348717187949781L;

	private static final String EMAIL_REGEXP = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";

	private static final String MOBILE_REGEXP = "1[35]{1}[0-9]{9}";

	private static final String MEMBERNO_REGEXP = "[0-9]{9}";

	private static final Pattern emailPattern = Pattern.compile(EMAIL_REGEXP);

	private static final Pattern mobilePattern = Pattern.compile(MOBILE_REGEXP);

	private static final Pattern membernoPattern = Pattern
			.compile(MEMBERNO_REGEXP);

	private UserService userService = null;

	private CartService cartService = null;

	private HttpServletRequest request = null;

	private Map session = null;

	private String id = null;

	private String password = null;

	private User user = null;

	private String requestURL = null;

	public void validate() {
		if (id == null || id.trim().equals(""))
			this.addFieldError("id", "用户名/会员号/手机号/邮箱不能为空!");
		if (password == null || password.trim().equals(""))
			this.addFieldError("password", "密码不能为空!");
	}

	public String execute() throws Exception {
		Cookie[] cookies = request.getCookies();
		for (Cookie c : cookies) {
			if (c.getName().equals("requestURL")) {
				requestURL = URLDecoder.decode(c.getValue(),"utf-8");
				break;
			}
		}

		// 先判断ID的类型.用户名?会员号?手机号?邮箱?
		if (user == null && emailPattern.matcher(id).matches()) {
			user = userService.loginByEmail(id, password);
		}
		if (user == null && mobilePattern.matcher(id).matches()) {
			user = userService.loginByMobile(id, password);
		}
		if (user == null && membernoPattern.matcher(id).matches()) {
			user = userService.loginByMemberno(id, password);
		}
		if (user == null) {
			user = userService.loginByRegName(id, password);
		}
		if (user != null) {
			session.put("user", user);
			// 更新用户购物车（如果存在的话）
			Cart cart = (Cart) session.get("cart");
			if (cart != null)
				cartService.rebuildCart(cart, user.getMemberId());
			if (requestURL == null)
				return SUCCESS;
			else
				return "requestURL";
		} else {
			this.addActionError("用户名/会员号/手机号/邮箱或者密码错误.");
			return "input";
		}

	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setSession(Map session) {
		this.session = session;

	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void setCartService(CartService cartService) {
		this.cartService = cartService;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public User getUser() {
		return user;
	}

	public String getRequestURL() {
		return requestURL;
	}

}
